package com.coocaa.bootup;

import android.content.ComponentName;
import android.content.Intent;
import android.os.RemoteException;

import com.alibaba.fastjson.JSONObject;
import com.coocaa.x.framework.app.BootUpRunnable;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LiteURI;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.XServiceManager;
import com.coocaa.x.service.litecontent.LiteContentServiceConnecter;
import com.tianci.appstore.ui.list.bootsetting.AppSelectedData;

/**
 * Created by lu on 15-7-8.
 */
public class AppBootUpRunnable extends BootUpRunnable {
    @Override
    public void run() {
        LogUtils.d("superx", "AppBootUpRunnable run");
        // a系列機器的奇葩問，恢復出廠後文件目錄權限被改成700，需要手動改變權限
        try {
            String path = "/data/data/" + CoocaaApplication.getContext().getPackageName();
            Runtime.getRuntime().exec("chmod -R 751 " + path);
        } catch (Exception e) {
        }
        //去掉桌面开机启动开关后，直接使用设置的自启应用
//        if (!startLauncher())
//        if (!SkyGeneralProperties.getBoolProperty("NEW_BOOT_FLOW"))
        startConfigLaunchApp();
    }

    private boolean startLauncher() {
        boolean autoStart = false;
        try {
            autoStart = XServiceManager.getLiteConfigService().getBooleanValue("autostart", false);
        } catch (RemoteException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        LogUtils.d("superx", "startLauncher  autoStart：" + autoStart);
        if (autoStart) {
            while (true) {
                try {
                    LogUtils.d("superx", "startLauncher wait.....");
                    Intent intent = new Intent();
                    intent.setComponent(new ComponentName("com.tianci.appstore",
                            "com.coocaa.launcher.EasyLauncherActivity"));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    try {
                        CoocaaApplication.getContext().startActivity(intent);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
//                    XServiceManager.getLitePMService().startAppByComponentName(
//                            new ComponentName("com.tianci.appstore",
//                                    "com.coocaa.launcher.EasyLauncherActivity"));
                    break;
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    LogUtils.d("superx", e.toString());
                }
            }
            return true;
        }
        return false;
    }

    private void startConfigLaunchApp() {
        try {
            LiteURI uri = LiteURI.create("setting://com.coocaa.app/startup_app");
            uri.addParams("action", "get");
            String result = LiteContentServiceConnecter.getService().call(uri.toString());
            LogUtils.d("superx", "startConfigLaunchApp：" + result);
            if (result != null) {
                AppSelectedData data = JSONObject.parseObject(result, AppSelectedData.class);
                if (data.error_code == 0 && data.resource != null && data.resource.total_count > 0) {
                    AppSelectedData.AppItem item = data.resource.resource_items.get(0);


                    Intent intent = new Intent();
                    intent.setComponent(new ComponentName(item.pkgname, item.activity));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    try {
                        CoocaaApplication.getContext().startActivity(intent);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //开机走尚峰的接口有问题，先直接暴力启动吧
//                    XServiceManager.getLitePMService().startAppByComponentName(
//                            new ComponentName(item.pkgname, item.activity));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
